//index.js
//获取应用实例
var sysinfo = wx.getSystemInfoSync(),
  statusHeight = sysinfo.statusBarHeight,
  isiOS = sysinfo.system.indexOf('iOS') > -1,
  navHeight;
if (!isiOS) {
  navHeight = 48;
} else {
  navHeight = 44;
}
const app = getApp()

var common = require('../../utils/util')

Page({
  data: {
    height: statusHeight,
    navHeight: navHeight,
    type: 'recommend',
    goodsList: [],
    loading: true,
    isMore: false,
    isUpdata: false,
    searchParams: {
      page: 1,
      mode: 'paginate',
      shop_id: 2,
      status: 1
    },
  },

  onLoad: function () {
    this.initialize()
    this.setData({
      baseUrl: common.baseUrl
    })
  },

  initialize() {
    let data = this.data.searchParams
    let array = this.data.goodsList
    common.http('app/shop/goods', 'GET', (res) => {
      if (res) {
        let page = this.data.searchParams.page
        if (res.data.length > 0) {
          res.data.forEach(function (item) {
            array.push(item)
          })
          this.setData({
            goodsList: array,
            'searchParams.page': res.current_page,
            isMore: false
          })
          if (page == 1) {
            app.globalData.indexList = res.data
            if (this.data.isUpdata) {
              this.setData({
                goodsList: app.globalData.indexList
              })
              if (this.data.goodsList.length > 0) {
                common.showToast('刷新成功')
                this.setData({
                  isUpdata: false
                })
              }
            }
          }
        } else {
          if (page > 1) {
            common.showToast('已经到底了')
            this.setData({
              isMore: false
            })
          }
        }
      }
      this.setData({
        loading: false
      })
    }, data)
  },

  onReady() {
    this.animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })

    this.move = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })
  },

  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    } 
  },

  animate: function () {
    let type = this.data.type;
    if (type == 'recommend') {
      this.animation.width('28px').step({
        duration: 500
      })
      this.move.left('45px').step({
        duration: 500
      })
    } else {
      this.animation.width('0px').step({
        duration: 500
      })
      this.move.left('82.5px').step({
        duration: 500
      })
    }
    this.setData({
      animation: this.animation.export(),
      move: this.move.export()
    });
  },

  handeToggle(e) {
    this.setData({
      type: e.currentTarget.dataset.type
    })
    this.animate()
  },

  touchstart(e) {
    this.setData({
      scrollStart: e.touches[0].pageX,
    })
  },

  scroll(e) {
    this.setData({
      scroll: e.detail.scrollLeft,
      scrollWidth: e.detail.scrollWidth
    })
  },

  touchend(e) {
    let scrollEnd = e.changedTouches[0].pageX
    if ((this.data.scrollStart - scrollEnd) >= 0) {
      console.log('左划')
      // if((this.data.scrollStart - scrollEnd) < 0)
      if (this.data.scroll < 50) {
        this.setData({
          type: "recommend"
        })
        this.animate()
      } else {
        this.setData({
          type: "collect"
        })
        this.animate()
      }
    } else {
      console.log('右划')
      if ((this.data.scrollWidth / 2 - this.data.scroll) > 50) {
        this.setData({
          type: "recommend"
        })
        this.animate()
      } else {
        this.setData({
          type: "collect"
        })
        this.animate()
      }
    }
  },

  // handleShop(e) {
  //   wx.navigateTo({
  //     url: '../direct/direct?id=' + e.currentTarget.dataset.id,
  //   })
  // },
  handleShop(e) {
    wx.navigateTo({
      url: '../shopInfo/shopInfo?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  handleUpdata: function () {
    let page = 1
    this.setData({
      isUpdata: true,
      'searchParams.page': page,
      goodsList: app.globalData.indexList
    })
    this.initialize()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  handleMore: function () {
    let page = this.data.searchParams.page
    this.setData({
      'searchParams.page': page + 1,
      isMore: true
    })
    this.initialize()
  },
})